package com.comunity_cloud.client.model.fetch_file;

import java.io.UnsupportedEncodingException;

import com.comunity_cloud.client.model.communication.packet.PeerPacketHeader;

public class FetchFileResponse extends PeerPacketHeader {

	private long offset;
	
	private String fileName;

	private byte[] fileNameBytes;
	
	public FetchFileResponse() {
		this.type = Type.FETCH_FILE_RESPONSE;
	}

	@Override
	public void convertToBytes() {
		byteBuffer.putLong(offset);
		byteBuffer.put(fileNameBytes);
	}
	
	@Override
	protected void fromBytes() {
		offset = byteBuffer.getLong();
		fileName = new String(bytes,byteBuffer.position(),headerLength - 8);
	}

	@Override
	protected int headerLength() {
		int length = 0;
		try {
			fileNameBytes = fileName.getBytes("UTF8");
			length = fileNameBytes.length;
		} catch (UnsupportedEncodingException e) {}
		return 8 + length;
	}

	public void setOffset(long offset) {
		this.offset = offset;
	}
	
	public long getOffset() {
		return offset;
	}
	
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

}
